﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using App;
using Domin;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using Microsoft.EntityFrameworkCore;
using JwT;
using Microsoft.AspNetCore.Authorization;

namespace UL.Controllers
{
    /// <summary>
    /// 商品显示
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodController : ControllerBase
    {
        private readonly IRApp db;

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="db"></param>
        public GoodController(IRApp db)
        {
            this.db = db;
        }
        /// <summary>
        /// 
        /// 
        /// 
        /// 用户显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[Authorize]
        public IActionResult Show(string nm,int Pindex=1,int Psize=2)
        {
            var ls = db.PShow().AsQueryable();
            if (!string.IsNullOrEmpty(nm))
            {
                ls = ls.Where(x => x.PName.Contains(nm));
            }
            PageList p = new PageList();
            p.PCount = ls.Count();
            p.PList = ls.OrderBy(x => x.PId).Skip((Pindex - 1) * Psize).Take(Psize).ToList();
             return Ok(new {data=p,msg="显示成功!" });
        }
        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult HAdd(string nm,int id)
        {
            db.HAdd(nm, id);
            return Ok(new {data=1,msg=" 用户角色添加成功!" });
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult File(IFormFile file, [FromServices] IWebHostEnvironment e)
        {
            if (file.Length==0)
            {
                return Ok(new {rest=-1,msg="图片上传失败!" });
            }
            string FileName = file.FileName;
            string FilePath = e.WebRootPath + @"/Imgs/" + FileName;
            using (FileStream fs = new FileStream(FilePath, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(new {kk=@"/Imgs/"+FileName,rest=1,msg="上传成功!" });
        }
        /// <summary>
        /// 用户权限显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]  
        public IActionResult OShow()
        {
            return Ok(new {data=db.OShow().ToList() });
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PAdd(Peoples peoples) 
        {
            db.Add(peoples);
            return Ok(new {data=1,msg="用户添加成功!" });
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PDel(string id)
        {
            db.Del(id);
            return Ok(new {data=1,msg="用户删除成功!" });
        }
        /// <summary>
        /// 菜单树形显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult MShow()
        {
            return Ok(new { data = db.MShow().ToList() });
        }
        /// <summary>
        /// 用户菜单显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UShow()
        {
            return Ok(new { data = db.UShow().ToList() });
        }
    }
}
